﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Mail;
using System.Text.RegularExpressions;

namespace IRL.Utilities
{

    public class Mics
    {
        public static void countReminder(Literal elem, string reminderType)
        {
            string market = "";
            if (HttpContext.Current.Request.Cookies["office"] != null)
                market = HttpContext.Current.Request.Cookies["office"].Value;
            object connStr = ConfigurationSettings.AppSettings["sqlConn"];
            using (SqlConnection conn = new SqlConnection(connStr.ToString()))
            {
                SqlCommand cmd = new SqlCommand("sp_ListReminders", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@userid", SqlDbType.Int).Value = HttpContext.Current.Request.Cookies["irl_id"].Value;
                cmd.Parameters.Add("@type", SqlDbType.VarChar, 10).Value = reminderType;
                if (market != "")
                    cmd.Parameters.Add("@market", SqlDbType.Int).Value = market;
                cmd.Parameters.Add("@count", SqlDbType.Bit).Value = 1;
                conn.Open();
                elem.Text = "(" + (int)cmd.ExecuteScalar() + ")";
            }
        }

        public static bool ValidateEmail(string email)
        {
            bool isValid = false;
            Regex r = new Regex("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
            Match m = r.Match(email);
            if (m.Success)
            {
                isValid = true;
            }
            return isValid;
        }

        public static void MailSend(string To, string Subject, string Body)
        {
            MailMessage mailer = new MailMessage();
            mailer.From = "INDOCHINA RESEARCH<noreply@indochinaresearch.com>";
            mailer.To = To;
            mailer.Subject = Subject;
            mailer.BodyFormat = MailFormat.Html;
            mailer.Body = Body;
            // setting
            mailer.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // Basic authentication
            mailer.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "chanmann");
            mailer.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "irlclim");
            // send mail
            SmtpMail.SmtpServer = "exchange";
            // TODO No email send
            //SmtpMail.Send(mailer);
        }


        public static void loadReminderCount(Literal elem)
        {
            string strScript = "<script language=\"javascript\">";
            object connStr = ConfigurationSettings.AppSettings["sqlConn"];
            int b_count, p_count;
            using (SqlConnection conn = new SqlConnection(connStr.ToString()))
            {
                SqlCommand cmd = new SqlCommand("sp_ListReminders", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@type", SqlDbType.VarChar, 10).Value = "Billing";
                cmd.Parameters.Add("@count", SqlDbType.Bit).Value = 1;
                conn.Open();
                b_count = (int)cmd.ExecuteScalar();
                // proposal reminder
                cmd.Parameters["@type"].Value = "Proposal";
                p_count = (int)cmd.ExecuteScalar();
                elem.Text = "<b>(" + (b_count + p_count) + ")</b>";
            }
            strScript += "menu2[0]=menu2[0].replace('%%COUNT%%', '(" + b_count + ")');";
            strScript += "menu2[1]=menu2[1].replace('%%COUNT%%', '(" + p_count + ")');";
            strScript += "<";
            strScript += "/";
            strScript += "script>";
            elem.Page.RegisterClientScriptBlock("RemindersCount", strScript);
        }

    }
}
